﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using LPrisaMVC.Entities;
using LPrisaMVC.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace LPrisaMVC.WebMVC4.Controllers
{
    public class ProveedorController : Controller
    {
        public ProveedorController()
        { 
        }
        
        //
        // GET: /Producto/

        [Dependency]
        public IProveedorBL proveedorBL{ get; set; }

        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index()
        {
            var proveedores = proveedorBL.GetFromProveedorByDescripcion("");
            return View(proveedores);
        }


        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index(string criterio = "")
        {
            var proveedores = proveedorBL.GetFromProveedorByDescripcion(criterio);
            return View(proveedores);
        }

        [Authorize(Roles = "admin, ventas")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //GET por default
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Create(PROVEEDOR proveedor)
        {
            try
            {
                proveedorBL.AddProveedor(proveedor);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Categoria/Edit/
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Edit(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Proveedor");
            }

            var proveedores = proveedorBL.GetFromProveedorById(id);
            if (proveedores == null)
            {
                return RedirectToAction("Index", "Proveedor");
            }

            return View(proveedores);
        }

        //
        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Edit(int id, PROVEEDOR proveedor)
        {
            try
            {
                proveedorBL.UpdateProveedor(proveedor);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        //[HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                proveedorBL.RemoveProveedor(id);

                return RedirectToAction("Index");
                //return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return RedirectToAction("Index");
                //return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
